package core.network.client2game.handler.club;

import business.global.club.Club;
import business.global.club.ClubMember;
import business.global.club.ClubMgr;
import business.global.union.Union;
import business.global.union.UnionMgr;
import business.player.Player;
import business.player.PlayerMgr;
import business.shareplayer.SharePlayer;
import business.shareplayer.SharePlayerMgr;
import cenum.ItemFlow;
import cenum.RoomTypeEnum;
import cenum.VisitSignEnum;
import com.ddm.server.common.CommLogD;
import com.ddm.server.common.utils.CommMath;
import com.ddm.server.common.utils.CommTime;
import com.ddm.server.websocket.def.ErrorCode;
import com.ddm.server.websocket.exception.WSException;
import com.ddm.server.websocket.handler.requset.WebSocketRequest;
import com.google.gson.Gson;
import core.db.entity.clarkGame.UnionDynamicBO;
import core.network.client2game.handler.PlayerHandler;
import core.network.http.proto.SData_Result;
import jsproto.c2s.cclass.club.Club_define;
import jsproto.c2s.cclass.club.Club_define.Club_Player_Status;
import jsproto.c2s.cclass.union.UnionDefine;
import jsproto.c2s.iclass.club.CClub_PlayerKickMultiZhongZhi;
import jsproto.c2s.iclass.club.CClub_PlayerStatusChange;
import org.apache.commons.collections.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 改变俱乐部玩家状态 批量删除
 *
 * @author zaf
 */
public class CClubKickPlayerMulti extends PlayerHandler {

    @Override
    public void handle(Player player, WebSocketRequest request, String message)
            throws WSException, IOException {
        final CClub_PlayerKickMultiZhongZhi req = new Gson().fromJson(message, CClub_PlayerKickMultiZhongZhi.class);

        ClubMember myClubMember = ClubMgr.getInstance().getClubMemberMgr().find(player.getPid(), req.clubId);
        if (null == myClubMember) {
            request.error(ErrorCode.NotAllow, "CClubKickPlayerMulti not find myClubMember");
            return;
        }
        if (myClubMember.isNotClubCreate()) {
            if(!myClubMember.checkHasPower(Club_define.Club_MEMBER_POWER.kickPower.value())){
                request.error(ErrorCode.NotAllow, "CClubKickPlayerMulti  isNotClubCreate");
                return;
            }
        }
        Club club = ClubMgr.getInstance().getClubListMgr().findClub(req.clubId);
        if (null == club) {
            request.error(ErrorCode.NotAllow, "CClubKickPlayerMulti not find club  clubId = " + req.clubId);
            return;
        }
        List<Long>  kickChoose=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(req.pidList)){
            for(Long pid:req.getPidList()){
                Player tempplayer = PlayerMgr.getInstance().getPlayer(pid);
                if (null == tempplayer) {
                   continue;
                }
                ClubMember tempMember = ClubMgr.getInstance().getClubMemberMgr().find(pid, req.clubId);
                if (null == tempMember) {
                    continue;
                }
                // 在游戏房间中
                if (checkExistRoom(request, club,  Club_Player_Status.PLAYER_TICHU.value(), tempMember)) {

                    continue;
                }
                kickChoose.add(pid);
            }
        }
        if(CollectionUtils.isNotEmpty(kickChoose)){
            for(Long pid:req.getPidList()){
                Player tempplayer = PlayerMgr.getInstance().getPlayer(pid);
                ClubMember tempMember = ClubMgr.getInstance().getClubMemberMgr().find(pid, req.clubId);
                tempMember.setStatus(tempplayer, club, Club_Player_Status.PLAYER_TICHU.value(), player.getPid(), false);
            }
        }
        List<Long> notKickList=req.getPidList().stream().filter(k->!kickChoose.contains(k)).collect(Collectors.toList());
        request.response(SData_Result.make(ErrorCode.Success,notKickList));
    }

    /**
     *
     * @param request
     * @param club
     * @param status
     * @param clubMember
     * @return
     */
    private boolean checkExistRoom(WebSocketRequest request, Club club, int status, ClubMember clubMember) {
        SharePlayer sharePlayer= SharePlayerMgr.getInstance().getSharePlayer(clubMember.getClubMemberBO().getPlayerID());
        if (Objects.isNull(sharePlayer)) {
            request.error(ErrorCode.Player_PidError, "clubMember.getClubMemberBO().getPlayerID()");
            return true;
        }
        //如果是踢人的话 判断这个玩家是否在亲友圈游戏中
        if (Club_Player_Status.PLAYER_JUJIE.value() == status || Club_Player_Status.PLAYER_TUICHU.value() == status
                || status == Club_Player_Status.PLAYER_TICHU.value()
                || status == Club_Player_Status.PLAYER_TICHU_CLOSE.value()
                || Club_Player_Status.PLAYER_JUJIEYAOQING.value() == status) {
            if(VisitSignEnum.ROOM.equals(sharePlayer.getSignEnum())&&sharePlayer.getRoomInfo().getClubId()==clubMember.getClubID()){
                CommLogD.error("player is in room play game == playId:{}", sharePlayer.getPlayerBO().getId());
                request.error(ErrorCode.CLUB_MEMBER_ROOM_ERROR, "CLUB_EXIST_PROMOTION_TICHU_ERROR");
                return true;
            }
        }
        return false;
    }


}
